import React from 'react';
import { observer, inject } from 'mobx-react';
import * as mobx from 'mobx';
import { WeaTop, WeaTab, WeaSelect, WeaTable, WeaFormItem, WeaInputSearch } from 'ecCom';
import { Button } from 'antd';

import * as _ from '../../util/common';
import { convert } from '../../util/analyzeUtil';
import WeaSelectTransfer from './wf-selectitem-browser/WeaSelectTranfer';

@inject('analyzeStore')
@observer
export default class linkageInfo extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            values: null
        }
    }
    render() {
        const { linkage_fieldOptions, linkage_typeValue, linkage_fieldTypeValue, linkage_fieldValue } = this.props.analyzeStore;
        const columns = [
            {
                title: '联动类型',
                dataIndex: 'linkageType',
                key: 'linkageType',
                width: "20%",
            }, {
                title: '触发字段',
                dataIndex: 'trgField',
                key: 'trgField',
                width: "20%",
            }, {
                title: '赋值字段',
                dataIndex: 'assignField',
                key: 'assignField',
                width: "20%",
            }, {
                title: '其他信息',
                dataIndex: 'otherInfo',
                key: 'otherInfo',
                width: "20%",
            }];

        return (
            <div className='wf-analyze-linkageInfo'>
                <div className='wf-linkageInfo-queryItems' >
                    <WeaFormItem
                        style={{ width: '30%' }}
                        label={'联动类型'}
                        labelCol={{ span: 6 }}
                        wrapperCol={{ span: 12 }}
                    >
                        <WeaSelect
                            options={[
                                { "key": "0", "showname": "", },
                                { "key": "1", "showname": "行规则" },
                                { "key": "2", "showname": "列赋值" },
                                { "key": "3", "showname": "sql赋值" },
                                { "key": "4", "showname": "日期计算赋值" },
                                { "key": "5", "showname": "字段赋值" },
                                { "key": "6", "showname": "选择框联动" },
                                { "key": "7", "showname": "显示属性联动" },
                                { "key": "8", "showname": "字段联动" },]
                            }
                            value={linkage_typeValue}
                            onChange={this.linkTypeChange.bind(this)}
                        />
                    </WeaFormItem>
                    <WeaFormItem
                        style={{ width: '70%' }}
                        label={'查询字段'}
                        labelCol={{ span: 3 }}
                        wrapperCol={{ span: 16 }}
                    >
                        <WeaSelect
                            style={{ width: '20%' }}
                            options={[
                                { "key": "0", "showname": "全部" },
                                { "key": "1", "showname": "触发字段" },
                                { "key": "2", "showname": "赋值字段" }
                            ]}
                            value={linkage_fieldTypeValue}
                            onChange={this.fieldTypeChange.bind(this)}
                        />
                        <WeaSelectTransfer
                            value={linkage_fieldValue}
                            options={linkage_fieldOptions}
                            onChange={this.fieldChange.bind(this)}
                        />
                    </WeaFormItem>
                </div>
                <WeaTable columns={columns} dataSource={this.getDataSource()} pagination={false} />
            </div>
        )
    }
    linkTypeChange = (value) => {
        const { linkTypeChange } = this.props.analyzeStore;
        linkTypeChange(value);
    }
    fieldTypeChange = (value) => {
        const { fieldTypeChange } = this.props.analyzeStore;
        fieldTypeChange(value);
    }
    fieldChange = (strArr) => {
        const { linkage_changeFieldValue } = this.props.analyzeStore;
        linkage_changeFieldValue(strArr);
    }
    getDataSource = () => {
        const WfForm = window.opener.WfForm;
        const layoutStore = WfForm.getLayoutStore();
        const { tableInfo } = layoutStore;
        const { rowCalCfg, fieldSqlCfg, fieldMathCfg, dataInputCfg, fieldDateCfg, viewAttrCfg, mainCalCfg } = layoutStore.linkageCfg;
        const { fieldMap, showFieldID, linkage_typeValue, linkage_fieldTypeValue, linkage_fieldValue } = this.props.analyzeStore;

        let dataSource = [];
        rowCalCfg && _.forIn(rowCalCfg, (value, key) => {
            if (key == 'rowCalAssignInfo') return false;
            let trgFieldObj = fieldMap.get(key);     //触发字段信息内容
            value.map(item => {
                let assignFieldObj = fieldMap.get(item.assignField);       //赋值字段信息内容
                dataSource.push(
                    {
                        linkageType: '行规则',
                        trgField: convert(key, trgFieldObj, showFieldID),
                        assignField: convert(item.assignField, assignFieldObj, showFieldID),
                        otherInfo: '',
                    }
                )
            })
        });
        mainCalCfg && _.forIn(mainCalCfg, (value, key) => {
            let trgFieldObj = fieldMap.get(key);                   //触发字段信息内容
            let assignFieldObj = fieldMap.get(value);              //赋值字段信息内容
            dataSource.push(
                {
                    linkageType: '列赋值',
                    trgField: convert(key, trgFieldObj, showFieldID),
                    assignField: convert(value, assignFieldObj, showFieldID),
                    otherInfo: '',
                }
            )
        });
        fieldSqlCfg && _.forIn(fieldSqlCfg, (value, key) => {
            if (key === 'emptyTriField') {
                value.map(item => {
                    let assignFieldObj = fieldMap.get(item.assignField);       //赋值字段信息内容
                    dataSource.push(
                        {
                            linkageType: 'sql赋值',
                            trgField: '无',
                            assignField: convert(item.assignField, assignFieldObj, showFieldID),
                            otherInfo: '',
                        }
                    )
                })
            } else {
                let trgFieldObj = fieldMap.get(key);     //字段信息内容
                value.map(item => {
                    let assignFieldObj = fieldMap.get(item.assignField);       //赋值字段信息内容
                    dataSource.push(
                        {
                            linkageType: 'sql赋值',
                            trgField: convert(key, trgFieldObj, showFieldID),
                            assignField: convert(item.assignField, assignFieldObj, showFieldID),
                            otherInfo: '',
                        }
                    )
                })
            }
        });
        fieldDateCfg && _.forIn(fieldDateCfg, (value, key) => {
            let trgFieldObj = fieldMap.get(key);     //字段信息内容
            value.map(item => {
                let assignFieldObj = fieldMap.get(item.assignField);       //赋值字段信息内容
                dataSource.push(
                    {
                        linkageType: '日期计算赋值',
                        trgField: convert(key, trgFieldObj, showFieldID),
                        assignField: convert(item.assignField, assignFieldObj, showFieldID),
                        otherInfo: '',
                    }
                )
            })
        });
        fieldMathCfg && _.forIn(fieldMathCfg, (value, key) => {
            let trgFieldObj = fieldMap.get(key);     //字段信息内容
            value.map(item => {
                let assignFieldObj = fieldMap.get(item.assignField);       //赋值字段信息内容
                dataSource.push(
                    {
                        linkageType: '字段赋值',
                        trgField: convert(key, trgFieldObj, showFieldID),
                        assignField: convert(item.assignField, assignFieldObj, showFieldID),
                        otherInfo: '',
                    }
                )
            })
        });
        dataInputCfg && _.forIn(dataInputCfg, (value, key) => {
            let trgFieldObj = fieldMap.get(key);     //字段信息内容
            value.map(item => {
                item.otherParams.assignfields.map(field => {
                    let assignFieldObj = fieldMap.get(field);       //赋值字段信息内容
                    dataSource.push(
                        {
                            linkageType: '字段联动',
                            trgField: convert(key, trgFieldObj, showFieldID),
                            assignField: convert(field, assignFieldObj, showFieldID),
                            otherInfo: '',
                        }
                    )
                })
            })
        });
        viewAttrCfg && _.forIn(viewAttrCfg, (value, key) => {
            value.relateFields && value.relateFields.map(item => {
                let trgField = key.slice(0, -2);
                let trgFieldObj = fieldMap.get(trgField);             //字段信息内容
                let assignField = item.slice(0, -2);
                let assignFieldObj = fieldMap.get(assignField);       //赋值字段信息内容
                dataSource.push(
                    {
                        linkageType: '显示属性联动',
                        trgField: convert(trgField, trgFieldObj, showFieldID),
                        assignField: convert(assignField, assignFieldObj, showFieldID),
                        otherInfo: '',
                    }
                )
            })
        });
        tableInfo && _.forIn(tableInfo, formInfo => {
            formInfo.fieldinfomap && _.forIn(formInfo.fieldinfomap, (value, key) => {
                let trgFieldObj = fieldMap.get(key);     //字段信息内容
                if (value.htmltype == 5 && value.selectattr.childfieldid > 0) {
                    let assignFieldObj = fieldMap.get(value.selectattr.childfieldid);       //赋值字段信息内容
                    dataSource.push(
                        {
                            linkageType: '选择框联动',
                            trgField: convert(key, trgFieldObj, showFieldID),
                            assignField: convert(value.selectattr.childfieldid, assignFieldObj, showFieldID),
                            otherInfo: '',
                        }
                    )
                }
            })
        })
        linkage_typeValue && (
            linkage_typeValue == '0' && null,
            linkage_typeValue == '1' && (dataSource = dataSource.filter(item => item.linkageType === '行规则')),
            linkage_typeValue == '2' && (dataSource = dataSource.filter(item => item.linkageType === '列赋值')),
            linkage_typeValue == '3' && (dataSource = dataSource.filter(item => item.linkageType === 'sql赋值')),
            linkage_typeValue == '4' && (dataSource = dataSource.filter(item => item.linkageType === '日期计算赋值')),
            linkage_typeValue == '5' && (dataSource = dataSource.filter(item => item.linkageType === '字段赋值')),
            linkage_typeValue == '6' && (dataSource = dataSource.filter(item => item.linkageType === '选择框联动')),
            linkage_typeValue == '7' && (dataSource = dataSource.filter(item => item.linkageType === '显示属性联动')),
            linkage_typeValue == '8' && (dataSource = dataSource.filter(item => item.linkageType === '字段联动'))
        );
        let fieldList = !!linkage_fieldValue ? linkage_fieldValue.split(",") : []
        fieldList.length == 0 ? null :
            (
                showFieldID ?
                    (
                        fieldList.map(fieldid => {
                            linkage_fieldTypeValue == 0 && (dataSource = dataSource.filter(item => item.trgField == fieldid || item.assignField == fieldid));
                            linkage_fieldTypeValue == 1 && (dataSource = dataSource.filter(item => item.trgField == fieldid));
                            linkage_fieldTypeValue == 2 && (dataSource = dataSource.filter(item => item.assignField == fieldid));
                        })
                    ) :
                    (
                        fieldList.map(fieldid => {
                            let fieldObj = fieldMap.get(fieldid);
                            let fieldName = convert(fieldid, fieldObj, false);
                            linkage_fieldTypeValue == 0 && (dataSource = dataSource.filter(item => item.trgField == fieldName || item.assignField == fieldName));
                            linkage_fieldTypeValue == 1 && (dataSource = dataSource.filter(item => item.trgField == fieldName));
                            linkage_fieldTypeValue == 2 && (dataSource = dataSource.filter(item => item.assignField == fieldName));
                        })
                    )
            )
        return dataSource;
    }
}